<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/3/6
 * Time: 下午6:07
 */

class Crown extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function login()
    {
        $scope='snsapi_base';
        $redirect_uri = site_url('/act/crown/logincallback');
        $this->load->library('openlogin/wechat');
        $this->wechat->set_scope($scope);
        $this->wechat->set_redirect_uri($redirect_uri);
        //跳转到引导页，静默授权
        $this->wechat->get_code();
    }

    public function logincallback()
    {
        //获取openid
        $this->load->library('openlogin/wechat');
        $this->wechat->get_access_token();
        $openid=$this->wechat->get_openid();
        //获取用户信息
        $this->load->library("wxinterface/Wxinterface");
        $sub = $this->wxinterface->getUserInfo($openid);
        if (!isset($sub['subscribe'])) die('系统繁忙，请耐心等候');
        if ($sub['subscribe']==1){
            if (!isset($_SESSION)) session_start();
            $url = $sub['headimgurl'];
            $url = substr($url,0,strrpos($url,'/')+1).'0.png';
//            //下载图片到本地
//            $this->load->model('image_model');
//            $data = $this->image_model->downloadfromurl($url,"/crown/sourceimg/",md5(time().uniqid()));
            $_SESSION['headimgurl']=$url;
            //跳转页面
            redirect('http://wap.nldou.com/crown/');
        }else{
            $this->load->view('act/crown/nosub');
        }
    }

    public function getHeadImg()
    {
        if (!isset($_SESSION)) session_start();
        if (isset($_SESSION['headimgurl'])){
            //下载图片到本地
            $this->load->model('image_model');
            $data = $this->image_model->downloadfromurl($_SESSION['headimgurl'],"/crown/sourceimg/",md5(time().uniqid()));
//            $this->success($_SESSION['headimgurl']);
            $this->success($data['url']);
        }else{$this->error();}

    }

    public function test(){
        $url = "http://thirdwx.qlogo.cn/mmopen/bVy2VQVTWzayMFu22F4uibOf3ZJdlSV05oDbTsoDJXKshDeZfRqnOkwxcgtKn80ApuZkL1ToKZd55USvrSGH4Jg/132";
        $url = substr($url,0,strrpos($url,'/')+1).'0.png';
        //下载图片到本地
        $this->load->model('image_model');
        $data = $this->image_model->downloadfromurl($url,"/crown/sourceimg/",md5(time().uniqid()));
        var_dump($data);
    }
}